﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Oliving.Edge.AuthHelper;
using Oliving.Edge.Common.Extensions;
using Oliving.Edge.Common.LogHelper;
using Oliving.Edge.IServices;
using Oliving.Edge.IServices.DTSN;
using Oliving.Edge.Model;
using Oliving.Edge.Model.DTSN;
using Oliving.Edge.Model.Models;
using Oliving.Edge.Model.ViewModels.DTSN;
using Oliving.Edge.Model.ViewModels.ReportDto;
using SqlSugar;

namespace Oliving.Edge.Controllers.DTSN.Report
{
    [ApiController]
    [Route("api/DTSNReport")]
    [Authorize(Permissions.Name)]
    public class DTSNReportController : OlivingControll
    {

        IMapper _mapper;
        private readonly IDTSN_CanningServices _DTSN_CanningServices;
        private readonly IDTSN_CanningRecordServices _CanningRecordServices;
        private readonly ILogger<DTSN_CanningController> _logger;


        public DTSNReportController(
        IHttpContextAccessor accessor, PermissionRequirement requirement,
        IMapper mapper, ILogger<DTSN_CanningController> logger,
        IDTSN_CanningServices DTSN_CanningServices,
        IDTSN_CanningRecordServices CanningRecordServices
    ) : base(accessor, requirement)
        {
            _logger = logger;
            _mapper = mapper;
            _DTSN_CanningServices = DTSN_CanningServices;
            _CanningRecordServices = CanningRecordServices;
        }


        [HttpGet]
        [AllowAnonymous]
        [Route("/api/DTSNReport/AccessApiByDateAsync")]
        public async Task<ChartShowMaterialView> AccessApiByDateAsync()
        {
            List<ChartMaterialDto> apiDates = new List<ChartMaterialDto>();

            DateTime dayNow = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59");
            DateTime ago = Convert.ToDateTime(DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd") + " 00:00:00");

            Expression<Func<DTSN_CanningRecord, bool>> whereExpression = c => true && (1 == 1);
            whereExpression = whereExpression.And(F => F.CR_Date <= dayNow && F.CR_Date >= ago);

            List<ChartDto> outLilst = await _CanningRecordServices.ReportChart(whereExpression);

            for (int i = 0; i < 7; i++)
            {
                string day = DateTime.Now.AddDays(-i).ToString("yyyy-MM-dd");
                apiDates.Add(new ChartMaterialDto
                {
                    date = day,
                    熟料 = outLilst.FindAll(F => F.type == 1 && Convert.ToDateTime(F.date).ToString("yyyy-MM-dd") == day).Sum(S => S.sum),
                    散料 = outLilst.FindAll(F => F.type == 2 && Convert.ToDateTime(F.date).ToString("yyyy-MM-dd") == day).Sum(S => S.sum)
                });
            }

            return new ChartShowMaterialView()
            {
                columns = new string[] { "date", "熟料", "散料" },
                rows = apiDates.OrderBy(d => d.date).ToList(),
            };
        }




        [HttpGet]
        [AllowAnonymous]
        [Route("/api/DTSNReport/ReportDayWuLiao")]
        public async Task<MessageModel<List<ChartViewDto_WuLiaoType>>> ReportDayWuLiao()
        {
            var data = new MessageModel<List<ChartViewDto_WuLiaoType>>();


            List<ChartMaterialDto> apiDates = new List<ChartMaterialDto>();

            DateTime dayNow = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59");
            DateTime ago = Convert.ToDateTime(DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd") + " 00:00:00");

            Expression<Func<DTSN_CanningRecord, bool>> whereExpression = c => true && (1 == 1);
            whereExpression = whereExpression.And(F => F.CR_Date <= dayNow && F.CR_Date >= ago);

            //List<ChartDto> outLilst = await _DTSN_CanningServices.ReportChart(whereExpression);
            List<ChartDto> outLilst = await _CanningRecordServices.ReportChart(whereExpression);

            List<ChartViewDto_WuLiaoType> outList = new List<ChartViewDto_WuLiaoType>();

            foreach (ChartDto dto in outLilst)
            {
                decimal clinker = 0;
                decimal BulkMaterial = 0;
                if (dto.type == 1) { clinker = dto.sum; }
                else if (dto.type == 2) { BulkMaterial = dto.sum; }

                if (outList.Count(C => C.date == dto.date.ToString("yyyy-MM-dd")) == 0)
                {
                    outList.Add(new ChartViewDto_WuLiaoType
                    {
                        date = dto.date.ToString("yyyy-MM-dd"),
                        BulkMaterial = BulkMaterial,
                        clinker = clinker
                    });
                }
                else
                {
                    ChartViewDto_WuLiaoType itemDto = outList.FirstOrDefault(F => F.date == dto.date.ToString("yyyy-MM-dd"));
                    if (itemDto != null)
                    {
                        itemDto.BulkMaterial = itemDto.BulkMaterial == 0 ? BulkMaterial : itemDto.BulkMaterial;
                        itemDto.clinker = itemDto.clinker == 0 ? clinker : itemDto.clinker;
                    }
                }
            }


            if (outList == null)
            {
                data.response = null;
                data.msg = "数据不存在！";
                data.success = false;
            }
            else
            {
                var dto = outList.OrderByDescending(D => D.date).ToList();
                data.response = dto;
                data.msg = "获取成功！";
                data.success = true;
            }

            return data;
        }




        [HttpGet]
        [AllowAnonymous]
        [Route("/api/DTSNReport/ReportTongDao")]
        public async Task<MessageModel<List<DTSN_ReportTongDaoDto>>> ReportTongDao(DateTime begin, DateTime end)
        {
            var data = new MessageModel<List<DTSN_ReportTongDaoDto>>();

            List<ChartMaterialDto> apiDates = new List<ChartMaterialDto>();

            if (begin == null || begin == DateTime.MinValue)
            {
                begin = Convert.ToDateTime(DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd") + " 00:00:00");
            }
            if (end == null || end == DateTime.MinValue)
            {
                end = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59");
            }



            Expression<Func<DTSN_CanningRecord, bool>> whereExpression = c => true && (1 == 1);


            whereExpression = whereExpression.And(F => F.CR_Date <= end && F.CR_Date >= begin);

            List<DTSN_ReportTongDaoDto> outLilst = await _CanningRecordServices.ReportTongDao(whereExpression);

            foreach (var dto in outLilst)
            {
                dto.TongDaoName = DateTime.Now.ObjToString();
            }


            data.response = outLilst;
            data.msg = "获取成功！";
            data.success = true;


            return data;
        }



    }
}